package web.java.download;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo01_Download extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1、获取文件路径
		String path=getServletContext().getRealPath("/upload");
		//2、得到文件夹对象
		File dir=new File(path);
		//3、使用map存储文件的全名称和简短名称
		Map<String,String> map=new HashMap<String,String>();
		if(dir.isDirectory()){
			//遍历文件夹
			String[] fileNames=dir.list();
			for(String longName:fileNames){
				String shortName=longName.substring(longName.lastIndexOf("#")+1, longName.length());
				map.put(longName, shortName);
			}
		}
		Set<Entry<String,String>> entrysName=map.entrySet();
		//4、显示到页面
		request.setAttribute("entrysName",entrysName);
		request.getRequestDispatcher("download.jsp").forward(request,response);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
}
